/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.CoreGraphics;

namespace ArtNotes
{
	/// <summary>
	/// This is the Flickr search tab
	/// Pretty simple, just an image and a text box
	/// and a button. You type in what to search
	/// for and then sit back and relax.
	/// </summary>
	public class ANImageSearchTabController : UIViewController
	{
		private ANFlickrResults anFlickrResults;
		
		public ANImageSearchTabController ()
		{
		}
		
		// Adapted from the monotouch samples
		private UIButton ImageButtonType (float xpos, float ypos)
		{
			var background = UIImage.FromFile("Images/btnSearch.png");
			var backgroundPressed = UIImage.FromFile("Images/btnSearchPressed.png");
			var frame = new RectangleF(xpos,ypos,48f,48f);
			
			var button = new UIButton(frame);
			var nbackground = background.StretchableImage(12,0);
			var nbackgroundPressed = backgroundPressed.StretchableImage(12,0);
			button.SetBackgroundImage(nbackground,UIControlState.Normal);
			button.SetBackgroundImage(nbackgroundPressed,UIControlState.Highlighted);

			button.Tag = 1;
			// To support reusable cells
			return button;
		}
		
		private UITextField searchField;
		private UIButton searchButton;
		
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();

			ANImageUtils imageUtils = new ANImageUtils();
			
			this.View.BackgroundColor = UIColor.FromPatternImage (
				UIImage.FromImage(
					imageUtils.overlayImage(
						UIImage.FromFile("Images/notepaper.png"),
						UIImage.FromFile("Images/search_tab_bg.png"),
						new RectangleF(0,0,320,480),
						new RectangleF(0,0,320,480),
						new SizeF(320,480)
						).CGImage
					)
				);
			
			searchField = new UITextField(new RectangleF (70f,100f,180f,25f));
			//searchField.BorderStyle = UITextBorderStyle.RoundedRect;
			//searchField.BackgroundColor = UIColor.Clear;
			searchField.ShouldReturn = searchFieldInputHandler;
			searchField.Font = UIFont.FromName ("Marker Felt", 20);
			this.View.AddSubview(searchField);
			
			searchButton = ImageButtonType(268f,90f);
			searchButton.TouchDown += delegate {
				Console.WriteLine("search button pressed : " + searchField.Text);
				anFlickrResults = new ANFlickrResults(searchField.Text);
				this.NavigationController.PushViewController(anFlickrResults, true);
			};
			this.View.AddSubview(searchButton);
		}
		
		
		public override void ViewWillAppear (bool animated)
		{
			//base.ViewWillAppear (animated);
			//ANFlickrSetup anFlickrSetup = new ANFlickrSetup();
			//this.View.AddSubview(anFlickrSetup.View);
			
			//this.View.AddSubview(anFlickrResults.View);
		}

		
		bool searchFieldInputHandler(UITextField tf)
		{
			tf.ResignFirstResponder();
			Console.WriteLine("Search Field Text: " + tf.Text);
			return true;
		}

	}
}
